package utils;

import cn.hutool.json.JSONUtil;
import entity.PmDeatil;
import entity.Pubmed;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PubmedUtils2 {
    public static List<Pubmed> parseHtml(List<Element> divList) {
        List<Pubmed> list = new ArrayList<>();
        for (Element doc : divList) {
            try {
                Pubmed pub = new Pubmed();
                //期刊or图书
                int type = 1;
                if (doc.html().startsWith("<bookdocument>")) {
                    type = 2;
                }
                pub.setType(type);
                if (doc.select("MedlineCitation").first() != null) {
                    int ArtType = 0;
                    Element tt = doc.selectFirst("MedlineCitation");
                    if (tt.attr("Status").equals("Publisher")) {
                        ArtType = 0;
                    } else if (tt.attr("Status").equals("In-Data-Review")) {
                        ArtType = 1;
                    } else if (tt.attr("Status").equals("MEDLINE")) {
                        ArtType = 2;
                    } else if (tt.attr("Status").equals("In-Process")) {
                        ArtType = 3;
                    } else if (tt.attr("Status").equals("PubMed-not-MEDLINE")) {
                        ArtType = 4;
                    } else {
                        //未知 后续可以用到
                        ArtType = 5;
                    }
                    pub.setArtType(ArtType);
                }

                //private Integer pmid;
                if (doc.select("PMID").size() > 0) {
                    Integer pmid = Integer.parseInt(doc.select("PMID").first().text());
                    pub.setPmid(pmid);
                }
                //private String title;
                if (doc.select("ArticleTitle").size() > 0) {
                    String title = doc.select("ArticleTitle").first().text();
                    pub.setArticleTitle(title);
                } else if (doc.select("VernacularTitle").size() > 0) {
                    String title = doc.select("VernacularTitle").first().text();
                    pub.setArticleTitle(title);
                } else if (doc.select("BookTitle").size() > 0) {
                    //图书
                    String title = doc.select("BookTitle").first().text();
                    pub.setArticleTitle(title);
                }
                //private String abstr;
                Elements anAbstract = doc.select("Abstract AbstractText");
                if (anAbstract.size() > 0) {
                    String abstr = "";
                    for (Element abs : anAbstract) {
                        String label = abs.attr("label");
                        if (label != null && !label.equals("")) {
                            label = "<strong class=\"sub-title\"> " + label + ": </strong>";
                        }
                        abstr += "<p>" + label + abs.text() + "</p>";
                    }
                    pub.setAbText(abstr);
                    //private String intro;
                    if (anAbstract != null && anAbstract.size() > 0) {
                        String intro = anAbstract.first().text();
                        if (intro.length() > 300) {
                            intro = intro.substring(0, 300) + "...";
                        }
                        pub.setIntro(intro);
                    }
                }
                //private String copyright;
                if (doc.select("CopyrightInformation").size() > 0) {
                    String copyright = doc.select("CopyrightInformation").first().text();
                    pub.setCopyright(copyright);
                }
                //private String keyword;
                Elements kws = doc.select("KeywordList Keyword");
                if (kws.size() > 0) {
                    String keyword = "";
                    for (Element kw : kws) {
                        keyword += "; " + kw.text();
                    }
                    if (keyword.length() > 2) {
                        pub.setKeyword("<p> <strong class=\"sub-title\"> Keywords: </strong> " + keyword.substring(2) + ". </p>");
                    }
                }
                //private String ptype;--找不到
                //期刊相关的
                //private String jour;
                //2020年9月18日13:14:15 老板不要了这个 改成了 就是jour字段取medlineTA 是吧
//                if (doc.select("Journal ISOAbbreviation").size() > 0) {
//                    String jour = doc.select("Journal ISOAbbreviation").first().text();
//                    pub.setJour(jour);
//                }
                if (doc.select("MedlineJournalInfo MedlineTA").size() > 0) {
                    String jour = doc.select("MedlineJournalInfo MedlineTA").first().text();
                    pub.setJour(jour);
                }
                //private String vip; volume issue page
                String vip = "";
                String vo = "";
                String is = "";
                String pages = "";
                if (doc.select("Journal Volume").size() > 0) {
                    vip += doc.select("Journal Volume").first().text();
                    vo = doc.select("Journal Volume").first().text();
                }
                if (doc.select("Journal Issue").size() > 0) {
                    vip += "(" + doc.select("Journal Issue").first().text() + ")";
                    is = doc.select("Journal Issue").first().text();
                }
                if (doc.select("Pagination MedlinePgn").size() > 0) {
                    vip += ", " + doc.select("Pagination MedlinePgn").first().text();
                    pages = doc.select("Pagination MedlinePgn").first().text();
                }
                if (StringUtils.isNotEmpty(vip)) {
                    if (vip.length() > 200) {
                        vip = "";
                    }
                }
                if (StringUtils.isNotEmpty(pages)) {
                    if (pages.length() > 200) {
                        pages = "";
                    }
                }

                pub.setVip(vip);
                pub.setVolume(vo);
                pub.setIssue(is);
                // 校验 2023年2月11日15:59:32  部分拿到数据是错误的 如pmid 26789871 页码是223-32 如果带有- 且后面页码小于前面的则进行获取其他维度
                if (StringUtils.isNotEmpty(pages) && pages.contains("-")) {
                    String[] split = pages.split("-");
                    if (split.length == 2) {
                        String aw = split[0];
                        String aw2 = split[1];
                        boolean numeric = StringUtils.isNumeric(aw);
                        boolean numeric2 = StringUtils.isNumeric(aw2);
                        if (numeric && numeric2) {
                            Integer start = Integer.parseInt(pages.split("-")[0]);
                            Integer end = 999999999;
                            try {
                                end = Integer.parseInt(pages.split("-")[1]);
                            } catch (Exception e) {

                            }
                            if (end < start) {
//                            SmallTool.printMess("错误页码 进行重置++pages++" + pages);
                                Element startPage = doc.selectFirst("StartPage");
                                Element endtPage = doc.selectFirst("EndPage");
                                if (startPage != null && endtPage != null) {
                                    pages = (startPage.childNode(0).toString() + "-" + endtPage.childNode(0).toString()).replace("\n", "").replace(" ", "").trim();
                                    pub.setPages(pages);
                                } else {
                                    pub.setPages(pages);
                                }
                            } else {
                                pub.setPages(pages);
                            }
                        } else {
                            pub.setPages(pages);
                        }
                    } else {
                        pub.setPages(pages);
                    }
                } else {
                    pub.setPages(pages);
                }

                //private String pub_data;
                //private String year;
                Elements pubDate_year = doc.select("PubDate Year");
                Elements medlinke_date = doc.select("Article Journal MedlineDate");
                Elements pubDate_month = doc.select("PubDate Month");
                Elements pubDate_day = doc.select("PubDate Day");
                String year = "";
                String month = "";
                String day = "";
                String medlineDate = "";
                String medlineYear = "";
                if (pubDate_year.size() > 0) {
                    year = pubDate_year.first().text();
                }
                if (pubDate_month.size() > 0) {
                    month = pubDate_month.first().text();
                }
                if (pubDate_day.size() > 0) {
                    day = pubDate_day.first().text();
                }
                if (medlinke_date.size() > 0) {
                    medlineDate = medlinke_date.first().text();
                    if (medlineDate.contains("-")) {
                        medlineYear = medlineDate.split("-")[0];
                    }


                }

                // 2021年11月17日14:25:40 部分数据是没有的 没有的话采用第二种方式进行获取
                // 部分数据是这样的 上面的没有的话去拿这部分数据
                // <PubMedPubDate PubStatus="pubmed">
                //                    <Year>2005</Year>
                //                    <Month>11</Month>
                //                    <Day>5</Day>
                //                    <Hour>9</Hour>
                //                    <Minute>0</Minute>
                //                </PubMedPubDate>
                //                <PubMedPubDate PubStatus="medline">
                //                    <Year>2006</Year>
                //                    <Month>2</Month>
                //                    <Day>16</Day>
                //                    <Hour>9</Hour>
                //                    <Minute>0</Minute>
                //                </PubMedPubDate>
                //                <PubMedPubDate PubStatus="entrez">
                //                    <Year>2005</Year>
                //                    <Month>11</Month>
                //                    <Day>5</Day>
                //                    <Hour>9</Hour>
                //                    <Minute>0</Minute>
                //                </PubMedPubDate>
                // 顺序  PubStatus="pubmed" -> PubStatus="entrez" -> PubStatus="medline"
                if (StringUtil.isNull(year)) {
                    // 目前数据 只要年 其他不管了
                    Element pubDate_year1 = doc.selectFirst("PubMedPubDate[PubStatus=pubmed]");
                    if (pubDate_year1 != null) {
                        Element year1 = pubDate_year1.selectFirst("Year");
                        if (year1 != null) {
                            year = year1.text();
                        }
                    }
                }
                if (StringUtil.isNull(year)) {
                    // 目前数据 只要年 其他不管了
                    Element pubDate_year1 = doc.selectFirst("PubMedPubDate[PubStatus=entrez]");
                    if (pubDate_year1 != null) {
                        Element year1 = pubDate_year1.selectFirst("Year");
                        if (year1 != null) {
                            year = year1.text();
                        }
                    }
                }
                if (StringUtil.isNull(year)) {
                    // 目前数据 只要年 其他不管了
                    Element pubDate_year1 = doc.selectFirst("PubMedPubDate[PubStatus=medline]");
                    if (pubDate_year1 != null) {
                        Element year1 = pubDate_year1.selectFirst("Year");
                        if (year1 != null) {
                            year = year1.text();
                        }
                    }
                }
                // 2022年5月17日10:49:48  目前发现两种 如果medline时间有且含有- 取 第一年为准
                // 2022年5月17日10:50:17  如果目前那个 Electronic 以他的年为准

                String ElectronicYear = "";
                Element ElectronicEle = doc.selectFirst("ArticleDate[DateType=Electronic]");
                if (ElectronicEle != null) {
                    ElectronicYear = ElectronicEle.selectFirst("Year").text();
                }


                String pub_data = year + "-" + month + "-" + day;
                pub.setPubDate(pub_data);
                pub.setMedlineYear(medlineYear);
                pub.setYearElec(ElectronicYear);
                pub.setMedlineDate(medlineDate);
                pub.setPubYear(year);
                //nlm_id
                if (doc.select("MedlineJournalInfo NlmUniqueID").size() > 0) {
                    String nlm_id = doc.select("MedlineJournalInfo NlmUniqueID").first().text();
                    pub.setMedNlmId(nlm_id);
                }

                //private List<PmDeatil.Affiliation> affs;
                //private List<PmDeatil.Author> author;
                Elements authorList = doc.select("AuthorList Author");
                if (authorList.size() > 0) {
                    Map<String, Integer> map = new HashMap<>();
                    int i = 1;
                    List<PmDeatil.Author> authors = new ArrayList<>();
                    List<PmDeatil.AuthorExtend> AuthorExtend = new ArrayList<>();
                    for (Element author : authorList) {
                        PmDeatil.Author au = new PmDeatil.Author();
                        PmDeatil.AuthorExtend authorExtend = new PmDeatil.AuthorExtend();
                        String lastName = "";
                        if (author.select("LastName").size() > 0) {
                            lastName = author.select("LastName").first().text();
                            authorExtend.setLastName(lastName);
                        } else {
                            authorExtend.setLastName(lastName);
                        }
                        String foreName = "";
                        if (author.select("ForeName").size() > 0) {
                            foreName = author.select("ForeName").first().text();
                            authorExtend.setForeName(foreName);
                        } else {
                            authorExtend.setForeName(foreName);
                        }
                        String initials = "";
                        if (author.select("Initials").size() > 0) {
                            initials = author.select("Initials").first().text();
                            authorExtend.setInitials(initials);
                        } else {
                            authorExtend.setInitials(initials);

                        }
                        String IdentifierSource = "";
                        if (author.select("Identifier").size() > 0) {
                            IdentifierSource = author.select("Identifier").first().text();
                            authorExtend.setIdentifierSource(IdentifierSource);
                        } else {
                            authorExtend.setIdentifierSource(IdentifierSource);

                        }

                        String collectiveName = "";
                        if (author.select("CollectiveName").size() > 0) {
                            collectiveName = author.select("CollectiveName").first().text();
                            authorExtend.setCollectiveName(collectiveName);

                        } else {
                            authorExtend.setCollectiveName(collectiveName);

                        }
                        // 新的一个属性 Suffix
                        String Suffix = "";
                        if (author.select("Suffix").size() > 0) {
                            Suffix = author.select("Suffix").first().text();
                            authorExtend.setSuffix(Suffix);
                        } else {
                            authorExtend.setSuffix(Suffix);

                        }

                        au.setName(foreName + " " + lastName);
                        au.setNameiso(lastName + " " + initials); //姓 和名 反了 原始备份
//                        au.setNameiso(initials + " " + lastName);
                        List<Integer> nums = new ArrayList<>();
                        List<String> Affiliation = new ArrayList<>();
                        if (author.select("AffiliationInfo").size() > 0) {
                            for (Element aff : author.select("AffiliationInfo")) {
                                String affiliation = aff.text();
                                Affiliation.add(affiliation);
                                if (!map.containsKey(affiliation)) {
                                    map.put(affiliation, i);
                                    nums.add(i);
                                    i++;
                                } else {
                                    nums.add(map.get(affiliation));
                                }
                            }
                        }
                        authorExtend.setAffiliation(Affiliation);
                        au.setNum(nums);
                        authors.add(au);
                        AuthorExtend.add(authorExtend);
                    }
                    List<PmDeatil.Affiliation> affs = new ArrayList<>();
                    map.forEach((k, v) -> {
                        PmDeatil.Affiliation aff = new PmDeatil.Affiliation();
                        aff.setName(k);
                        aff.setNum(v);
                        affs.add(aff);
                    });
                    pub.setAuthor(JSONUtil.toJsonStr(authors));
                    pub.setAuthorExtend(JSONUtil.toJsonStr(AuthorExtend));
                    pub.setAffs(JSONUtil.toJsonStr(affs));
                }
                //private String pmcid;
                //private String doi;
                Elements articleId = doc.select("ArticleIdList ArticleId");
                if (articleId.size() > 0) {
                    for (Element ids : articleId) {
                        if ("doi".equals(ids.attr("IdType"))) {
                            pub.setDoi(ids.text());
                        }
                        if ("pmc".equals(ids.attr("IdType"))) {
                            pub.setPmcid(ids.text());
                        }
                    }
                }
                //private List<PmDeatil.AssociatedData> pts;
                Elements pTypes = doc.select("PublicationType");
                if (pTypes.size() > 0) {
                    List<PmDeatil.AssociatedData> pts = new ArrayList<>();
                    for (Element pType : pTypes) {
                        PmDeatil.AssociatedData pt = new PmDeatil.AssociatedData();
                        pt.setType(2);
                        pt.setTitle(pType.text());
                        List<PmDeatil.Items> hrefs = new ArrayList<>();
                        hrefs.add(new PmDeatil.Items("Search in PubMed", "?term=" + StringUtil.encodeUrl(pType.text() + "[pt]")));
                        hrefs.add(new PmDeatil.Items("Search in MeSH", "https://www.ncbi.nlm.nih.gov/mesh?term=" + pType.text()));
                        hrefs.add(new PmDeatil.Items("Add to Search", pType.text() + "[pt]"));
                        pt.setHrefs(hrefs);
                        pts.add(pt);
                    }
                    pub.setPts(JSONUtil.toJsonStr(pts));
                }
                //private List<PmDeatil.AssociatedData> meshs;
                //一个展示用，一个程序用

                Elements mes = doc.select("MeshHeadingList MeshHeading");
                if (mes.size() > 0) {
                    String meshHeading = "";
                    String meshHeadingy = "";
                    List<PmDeatil.AssociatedData> meshs = new ArrayList<>();
                    for (Element el : mes) {
                        //展示用
                        String dName = "";
                        if (el.select("DescriptorName").size() > 0) {
                            dName = el.select("DescriptorName").text();
                        }
                        if (el.select("QualifierName").size() > 0) {
                            for (Element qn : el.select("QualifierName")) {
                                PmDeatil.AssociatedData mesh = new PmDeatil.AssociatedData();
                                mesh.setType(2);
                                String qname = dName + " / " + qn.text();
                                mesh.setTitle(qname);
                                String sPubmed = dName + "/" + qn.text() + "[MESH]";
                                if (qn.attr("MajorTopicYN").equals("Y")) {
                                    mesh.setTitle(qname + "*");
                                    sPubmed = dName + "/" + qn.text() + "[MAJR]";
                                }
                                List<PmDeatil.Items> hrefs = new ArrayList<>();
                                hrefs.add(new PmDeatil.Items("Search in PubMed", "?term=" + StringUtil.encodeUrl(sPubmed)));
                                hrefs.add(new PmDeatil.Items("Search in MeSH", "https://www.ncbi.nlm.nih.gov/mesh?term=" + dName.replaceAll(" ", "+")));
                                hrefs.add(new PmDeatil.Items("Add to Search", sPubmed));
                                mesh.setHrefs(hrefs);
                                meshs.add(mesh);
                            }
                        } else {
                            PmDeatil.AssociatedData mesh = new PmDeatil.AssociatedData();
                            mesh.setType(2);
                            mesh.setTitle(dName);
                            List<PmDeatil.Items> hrefs = new ArrayList<>();
                            hrefs.add(new PmDeatil.Items("Search in PubMed", "?term=" + StringUtil.encodeUrl(dName + "[MESH]")));
                            hrefs.add(new PmDeatil.Items("Search in MeSH", "https://www.ncbi.nlm.nih.gov/mesh?term=" + dName));
                            hrefs.add(new PmDeatil.Items("Add to Search", dName + "[MESH]"));
                            mesh.setHrefs(hrefs);
                            meshs.add(mesh);
                        }


                        String m1 = "";
                        String m2 = "";
                        meshHeading += ";" + el.select("DescriptorName").text();
                        //999999

                        String param = "";
                        Element descriptorName = el.select("DescriptorName").first();
                        if (descriptorName.attr("MajorTopicYN").equals("Y")) {
                            param = el.select("DescriptorName").text() + "*";
                        } else {
                            param = el.select("DescriptorName").text();
                        }
                        meshHeadingy += ";" + param;
                        //00000
//                        meshHeadingy+= ";" + el.select("DescriptorName").text();
                        Elements ss = el.select("QualifierName");
                        for (Element els : ss) {
                            m1 += "/" + els.text();
                            if (els.attr("MajorTopicYN").equals("Y")) {
                                m2 += "/*" + els.text();
                            } else {
                                m2 += "/" + els.text();
                            }
                        }
                        if (m1.length() > 0) {
                            m1 = m1.substring(1);
                            meshHeading += "|" + m1;
                        }
                        if (m2.length() > 0) {
                            m2 = m2.substring(1);
                            meshHeadingy += "|" + m2;
                        }
                    }
                    if (StringUtil.isNotNull(meshHeading)) {
                        pub.setMeshHeading(meshHeading.substring(1));
                        pub.setMeshHeadingy(meshHeadingy.substring(1));
                    }
                    pub.setMeshs(JSONUtil.toJsonStr(meshs));
                }
                //private List<PmDeatil.AssociatedData> subs;
                Elements chemical = doc.select("ChemicalList Chemical");
                if (chemical.size() > 0) {
                    List<PmDeatil.AssociatedData> subs = new ArrayList<>();
                    for (Element chem : chemical) {
                        if (chem.select("NameOfSubstance").size() > 0) {
                            String substance = chem.select("NameOfSubstance").text();
                            PmDeatil.AssociatedData sub = new PmDeatil.AssociatedData();
                            sub.setType(2);
                            sub.setTitle(substance);
                            List<PmDeatil.Items> hrefs = new ArrayList<>();
                            hrefs.add(new PmDeatil.Items("Search in PubMed", "?term=" + StringUtil.encodeUrl(substance + "[nm]")));
                            hrefs.add(new PmDeatil.Items("Search in MeSH", "https://www.ncbi.nlm.nih.gov/mesh?term=" + substance));
                            hrefs.add(new PmDeatil.Items("Add to Search", substance + "[nm]"));
                            sub.setHrefs(hrefs);
                            subs.add(sub);
                        }
                    }
                    pub.setSubs(JSONUtil.toJsonStr(subs));
                }
                //private List<PmDeatil.AssociatedData> grants;
                Elements grantList = doc.select("GrantList Grant");
                if (grantList.size() > 0) {
                    List<PmDeatil.AssociatedData> grants = new ArrayList<>();
                    for (Element gran : grantList) {
                        PmDeatil.AssociatedData grant = new PmDeatil.AssociatedData();
                        grant.setType(1);
                        String name = StringUtil.filterHtml(gran.toString().replace("<grant>", "").replace("</grant>", ""));
                        name = StringUtil.filterHtml2(name);
                        name = name.replaceAll("\n", "").replaceAll("  ", "").substring(1);
                        grant.setTitle(name);
                        grant.setHref("/?term=" + StringUtil.encodeUrl(name + "[Grant Number]"));
                        grants.add(grant);
                    }
                    pub.setGrants(JSONUtil.toJsonStr(grants));
                }
                //private List<PmDeatil.AssociatedData> assData;--没法玩啊 无法判断直接跳转还是下拉跳转
                //private List<PmDeatil.FullText> fulltext;--找不到
                //private String source;
                String source = "";
                List<PmDeatil.Author> authors = JSONUtil.toList(JSONUtil.parseArray(pub.getAuthor()), PmDeatil.Author.class);
                if (authors != null && authors.size() > 0) {
                    source = authors.get(0).getName() + ",";
                }
                if (pub.getJour() != null && !pub.getJour().equals("")) {
                    source = source + pub.getJour() + " ";
                }
                if (pub.getPubYear() != null && !pub.getPubYear().equals("")) {
                    source = source + pub.getPubYear() + ".";
                }
                pub.setSource(source);
                //lan
                Elements languages = doc.select("Language");
                if (languages.size() > 0) {
                    String lan = "";
                    for (Element el : languages) {
                        lan += ";" + el.text();
                    }
                    if (lan.length() > 0) {
                        pub.setLan(lan.substring(1));
                    }
                }
                list.add(pub);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    // 2022年5月30日15:02:34 这个方法主要是添加了作者地址和判断是不是中国发文
    public static List<Pubmed> parseHtmlAddAuAdd(List<Element> divList) {
        List<Pubmed> list = new ArrayList<>();
        for (Element doc : divList) {
            try {
                Pubmed pub = new Pubmed();
                //期刊or图书
                int type = 1;
                if (doc.html().startsWith("<bookdocument>")) {
                    type = 2;
                }
                pub.setType(type);
                if (doc.select("MedlineCitation").first() != null) {
                    int ArtType = 0;
                    Element tt = doc.selectFirst("MedlineCitation");
                    if (tt.attr("Status").equals("Publisher")) {
                        ArtType = 0;
                    } else if (tt.attr("Status").equals("In-Data-Review")) {
                        ArtType = 1;
                    } else if (tt.attr("Status").equals("MEDLINE")) {
                        ArtType = 2;
                    } else if (tt.attr("Status").equals("In-Process")) {
                        ArtType = 3;
                    } else if (tt.attr("Status").equals("PubMed-not-MEDLINE")) {
                        ArtType = 4;
                    } else {
                        //未知 后续可以用到
                        ArtType = 5;
                    }
                    pub.setArtType(ArtType);
                }

                //private Integer pmid;
                if (doc.select("PMID").size() > 0) {
                    Integer pmid = Integer.parseInt(doc.select("PMID").first().text());
                    pub.setPmid(pmid);
                }
                //private String title;
                if (doc.select("ArticleTitle").size() > 0) {
                    String title = doc.select("ArticleTitle").first().text();
                    pub.setArticleTitle(title);
                } else if (doc.select("VernacularTitle").size() > 0) {
                    String title = doc.select("VernacularTitle").first().text();
                    pub.setArticleTitle(title);
                } else if (doc.select("BookTitle").size() > 0) {
                    //图书
                    String title = doc.select("BookTitle").first().text();
                    pub.setArticleTitle(title);
                }
                //private String abstr;
                Elements anAbstract = doc.select("Abstract AbstractText");
                if (anAbstract.size() > 0) {
                    String abstr = "";
                    for (Element abs : anAbstract) {
                        String label = abs.attr("label");
                        if (label != null && !label.equals("")) {
                            label = "<strong class=\"sub-title\"> " + label + ": </strong>";
                        }
                        abstr += "<p>" + label + abs.text() + "</p>";
                    }
                    pub.setAbText(abstr);
                    //private String intro;
                    if (anAbstract != null && anAbstract.size() > 0) {
                        String intro = anAbstract.first().text();
                        if (intro.length() > 300) {
                            intro = intro.substring(0, 300) + "...";
                        }
                        pub.setIntro(intro);
                    }
                }
                //private String copyright;
                if (doc.select("CopyrightInformation").size() > 0) {
                    String copyright = doc.select("CopyrightInformation").first().text();
                    pub.setCopyright(copyright);
                }
                //private String keyword;
                Elements kws = doc.select("KeywordList Keyword");
                if (kws.size() > 0) {
                    String keyword = "";
                    for (Element kw : kws) {
                        keyword += "; " + kw.text();
                    }
                    if (keyword.length() > 2) {
                        pub.setKeyword("<p> <strong class=\"sub-title\"> Keywords: </strong> " + keyword.substring(2) + ". </p>");
                    }
                }
                //private String ptype;--找不到
                //期刊相关的
                //private String jour;
                //2020年9月18日13:14:15 老板不要了这个 改成了 就是jour字段取medlineTA 是吧
//                if (doc.select("Journal ISOAbbreviation").size() > 0) {
//                    String jour = doc.select("Journal ISOAbbreviation").first().text();
//                    pub.setJour(jour);
//                }
                if (doc.select("MedlineJournalInfo MedlineTA").size() > 0) {
                    String jour = doc.select("MedlineJournalInfo MedlineTA").first().text();
                    pub.setJour(jour);
                }
                //private String vip; volume issue page
                String vip = "";
                String vo = "";
                String is = "";
                String pages = "";
                if (doc.select("Journal Volume").size() > 0) {
                    vip += doc.select("Journal Volume").first().text();
                    vo = doc.select("Journal Volume").first().text();
                }
                if (doc.select("Journal Issue").size() > 0) {
                    vip += "(" + doc.select("Journal Issue").first().text() + ")";
                    is = doc.select("Journal Issue").first().text();
                }
                if (doc.select("Pagination MedlinePgn").size() > 0) {
                    vip += ", " + doc.select("Pagination MedlinePgn").first().text();
                    pages = doc.select("Pagination MedlinePgn").first().text();
                }
                if (StringUtils.isNotEmpty(vip)) {
                    if (vip.length() > 200) {
                        vip = "";
                    }
                }
                if (StringUtils.isNotEmpty(pages)) {
                    if (pages.length() > 200) {
                        pages = "";
                    }
                }

                pub.setVip(vip);
                pub.setVolume(vo);
                pub.setIssue(is);
                // 校验 2023年2月11日15:59:32  部分拿到数据是错误的 如pmid 26789871 页码是223-32 如果带有- 且后面页码小于前面的则进行获取其他维度
                if (StringUtils.isNotEmpty(pages) && pages.contains("-")) {
                    String[] split = pages.split("-");
                    if (split.length == 2) {
                        String aw = split[0];
                        String aw2 = split[1];
                        boolean numeric = StringUtils.isNumeric(aw);
                        boolean numeric2 = StringUtils.isNumeric(aw2);
                        if (numeric && numeric2) {
                            Integer start = Integer.parseInt(pages.split("-")[0]);
                            Integer end = 999999999;
                            try {
                                end = Integer.parseInt(pages.split("-")[1]);
                            } catch (Exception e) {

                            }
                            if (end < start) {
//                            SmallTool.printMess("错误页码 进行重置++pages++" + pages);
                                Element startPage = doc.selectFirst("StartPage");
                                Element endtPage = doc.selectFirst("EndPage");
                                if (startPage != null && endtPage != null) {
                                    pages = (startPage.childNode(0).toString() + "-" + endtPage.childNode(0).toString()).replace("\n", "").replace(" ", "").trim();
                                    pub.setPages(pages);
                                } else {
                                    pub.setPages(pages);
                                }
                            } else {
                                pub.setPages(pages);
                            }
                        } else {
                            pub.setPages(pages);
                        }
                    } else {
                        pub.setPages(pages);
                    }
                } else {
                    pub.setPages(pages);
                }
                //private String pub_data;
                //private String year;
                Elements pubDate_year = doc.select("PubDate Year");
                Elements medlinke_date = doc.select("Article Journal MedlineDate");
                Elements pubDate_month = doc.select("PubDate Month");
                Elements pubDate_day = doc.select("PubDate Day");
                String year = "";
                String month = "";
                String day = "";
                String medlineDate = "";
                String medlineYear = "";
                if (pubDate_year.size() > 0) {
                    year = pubDate_year.first().text();
                }
                if (pubDate_month.size() > 0) {
                    month = pubDate_month.first().text();
                }
                if (pubDate_day.size() > 0) {
                    day = pubDate_day.first().text();
                }
                if (medlinke_date.size() > 0) {
                    medlineDate = medlinke_date.first().text();
                    if (medlineDate.contains("-")) {
                        medlineYear = medlineDate.split("-")[0];
                    }


                }

                // 2021年11月17日14:25:40 部分数据是没有的 没有的话采用第二种方式进行获取
                // 部分数据是这样的 上面的没有的话去拿这部分数据
                // <PubMedPubDate PubStatus="pubmed">
                //                    <Year>2005</Year>
                //                    <Month>11</Month>
                //                    <Day>5</Day>
                //                    <Hour>9</Hour>
                //                    <Minute>0</Minute>
                //                </PubMedPubDate>
                //                <PubMedPubDate PubStatus="medline">
                //                    <Year>2006</Year>
                //                    <Month>2</Month>
                //                    <Day>16</Day>
                //                    <Hour>9</Hour>
                //                    <Minute>0</Minute>
                //                </PubMedPubDate>
                //                <PubMedPubDate PubStatus="entrez">
                //                    <Year>2005</Year>
                //                    <Month>11</Month>
                //                    <Day>5</Day>
                //                    <Hour>9</Hour>
                //                    <Minute>0</Minute>
                //                </PubMedPubDate>
                // 顺序  PubStatus="pubmed" -> PubStatus="entrez" -> PubStatus="medline"
                if (StringUtil.isNull(year)) {
                    // 目前数据 只要年 其他不管了
                    Element pubDate_year1 = doc.selectFirst("PubMedPubDate[PubStatus=pubmed]");
                    if (pubDate_year1 != null) {
                        Element year1 = pubDate_year1.selectFirst("Year");
                        if (year1 != null) {
                            year = year1.text();
                        }
                    }
                }
                if (StringUtil.isNull(year)) {
                    // 目前数据 只要年 其他不管了
                    Element pubDate_year1 = doc.selectFirst("PubMedPubDate[PubStatus=entrez]");
                    if (pubDate_year1 != null) {
                        Element year1 = pubDate_year1.selectFirst("Year");
                        if (year1 != null) {
                            year = year1.text();
                        }
                    }
                }
                if (StringUtil.isNull(year)) {
                    // 目前数据 只要年 其他不管了
                    Element pubDate_year1 = doc.selectFirst("PubMedPubDate[PubStatus=medline]");
                    if (pubDate_year1 != null) {
                        Element year1 = pubDate_year1.selectFirst("Year");
                        if (year1 != null) {
                            year = year1.text();
                        }
                    }
                }
                // 2022年5月17日10:49:48  目前发现两种 如果medline时间有且含有- 取 第一年为准
                // 2022年5月17日10:50:17  如果目前那个 Electronic 以他的年为准

                String ElectronicYear = "";
                Element ElectronicEle = doc.selectFirst("ArticleDate[DateType=Electronic]");
                if (ElectronicEle != null) {
                    ElectronicYear = ElectronicEle.selectFirst("Year").text();
                }


                String pub_data = year + "-" + month + "-" + day;
                pub.setPubDate(pub_data);
                pub.setMedlineYear(medlineYear);
                pub.setYearElec(ElectronicYear);
                pub.setMedlineDate(medlineDate);
                pub.setPubYear(year);
                //nlm_id
                if (doc.select("MedlineJournalInfo NlmUniqueID").size() > 0) {
                    String nlm_id = doc.select("MedlineJournalInfo NlmUniqueID").first().text();
                    pub.setMedNlmId(nlm_id);
                }

                //private List<PmDeatil.Affiliation> affs;
                //private List<PmDeatil.Author> author;
                Elements authorList = doc.select("AuthorList Author");
                if (authorList.size() > 0) {
                    Map<String, Integer> map = new HashMap<>();
                    int i = 1;
                    List<PmDeatil.Author> authors = new ArrayList<>();
                    List<PmDeatil.AuthorExtend> AuthorExtend = new ArrayList<>();
                    for (Element author : authorList) {
                        PmDeatil.Author au = new PmDeatil.Author();
                        PmDeatil.AuthorExtend authorExtend = new PmDeatil.AuthorExtend();
                        String lastName = "";
                        if (author.select("LastName").size() > 0) {
                            lastName = author.select("LastName").first().text();
                            authorExtend.setLastName(lastName);
                        } else {
                            authorExtend.setLastName(lastName);
                        }
                        String foreName = "";
                        if (author.select("ForeName").size() > 0) {
                            foreName = author.select("ForeName").first().text();
                            authorExtend.setForeName(foreName);
                        } else {
                            authorExtend.setForeName(foreName);
                        }
                        String initials = "";
                        if (author.select("Initials").size() > 0) {
                            initials = author.select("Initials").first().text();
                            authorExtend.setInitials(initials);
                        } else {
                            authorExtend.setInitials(initials);

                        }
                        String IdentifierSource = "";
                        if (author.select("Identifier").size() > 0) {
                            IdentifierSource = author.select("Identifier").first().text();
                            authorExtend.setIdentifierSource(IdentifierSource);
                        } else {
                            authorExtend.setIdentifierSource(IdentifierSource);

                        }

                        String collectiveName = "";
                        if (author.select("CollectiveName").size() > 0) {
                            collectiveName = author.select("CollectiveName").first().text();
                            authorExtend.setCollectiveName(collectiveName);

                        } else {
                            authorExtend.setCollectiveName(collectiveName);

                        }
                        // 新的一个属性 Suffix
                        String Suffix = "";
                        if (author.select("Suffix").size() > 0) {
                            Suffix = author.select("Suffix").first().text();
                            authorExtend.setSuffix(Suffix);
                        } else {
                            authorExtend.setSuffix(Suffix);

                        }

                        au.setName(foreName + " " + lastName);
                        au.setNameiso(lastName + " " + initials); //姓 和名 反了 原始备份
//                        au.setNameiso(initials + " " + lastName);
                        List<Integer> nums = new ArrayList<>();
                        List<String> Affiliation = new ArrayList<>();
                        if (author.select("AffiliationInfo").size() > 0) {
                            for (Element aff : author.select("AffiliationInfo")) {
                                String affiliation = aff.text();
                                Affiliation.add(affiliation);
                                if (!map.containsKey(affiliation)) {
                                    map.put(affiliation, i);
                                    nums.add(i);
                                    i++;
                                } else {
                                    nums.add(map.get(affiliation));
                                }
                            }
                        }
                        authorExtend.setAffiliation(Affiliation);
                        au.setNum(nums);
                        authors.add(au);
                        AuthorExtend.add(authorExtend);
                    }
                    List<PmDeatil.Affiliation> affs = new ArrayList<>();
                    map.forEach((k, v) -> {
                        PmDeatil.Affiliation aff = new PmDeatil.Affiliation();
                        aff.setName(k);
                        aff.setNum(v);
                        affs.add(aff);
                    });
                    pub.setAuthor(JSONUtil.toJsonStr(authors));
                    pub.setAuthorExtend(JSONUtil.toJsonStr(AuthorExtend));
                    pub.setAffs(JSONUtil.toJsonStr(affs));
                }

                //2022年5月30日15:05:32 添加作者地址
                try {
                    if (authorList.size() > 0) {
                        Map<String, Integer> map = new HashMap<>();//作者地址 ,第几个作者
                        int i = 0;
                        List<AuthorExtend> AuthorExtend = new ArrayList<>();
                        for (Element author : authorList) {
                            ++i;
                            AuthorExtend authorExtend = new AuthorExtend();
                            String lastName = "";
                            if (author.select("LastName").size() > 0) {
                                lastName = author.select("LastName").first().text();
                                authorExtend.setLastName(lastName);
                            }
                            String foreName = "";
                            if (author.select("ForeName").size() > 0) {
                                foreName = author.select("ForeName").first().text();
                                authorExtend.setForeName(foreName);

                            }
                            String initials = "";
                            if (author.select("Initials").size() > 0) {
                                initials = author.select("Initials").first().text();
                                authorExtend.setInitials(initials);

                            }
                            String IdentifierSource = "";
                            if (author.select("Identifier").size() > 0) {
                                IdentifierSource = author.select("Identifier").first().text();
                                authorExtend.setIdentifierSource(IdentifierSource);

                            }

                            List<Integer> nums = new ArrayList<>();
                            List<String> Affiliation = new ArrayList<>();
                            if (author.select("AffiliationInfo").size() > 0) {
                                for (Element aff : author.select("AffiliationInfo")) {
                                    String affiliation = aff.text();
                                    Affiliation.add(affiliation);
                                    if (!map.containsKey(affiliation)) {
                                        map.put(affiliation, i);
                                        nums.add(i);

                                    } else {
                                        nums.add(map.get(affiliation));
                                    }
                                }
                            }
                            authorExtend.setNumber(i);
                            authorExtend.setAffiliation(Affiliation);
                            if (StringUtil.isNotNull(authorExtend.getForeName())) {
                                AuthorExtend.add(authorExtend);
                            }
                        }

                        pub.setAuthorAdd(JSONUtil.toJsonStr(AuthorExtend));
                    }

                } catch (Exception e) {

                }
                if (StringUtils.isNotEmpty(pub.getAuthorAdd())) {
                    if (StringUtil.isChina(pub.getAuthorAdd())) {
                        pub.setIschina(1);
                    } else {
                        pub.setIschina(0);
                    }
                }
                //
                // authorextend

                //private String pmcid;
                //private String doi;
                Elements articleId = doc.select("ArticleIdList ArticleId");
                if (articleId.size() > 0) {
                    for (Element ids : articleId) {
                        if ("doi".equals(ids.attr("IdType"))) {
                            pub.setDoi(ids.text());
                        }
                        if ("pmc".equals(ids.attr("IdType"))) {
                            pub.setPmcid(ids.text());
                        }
                    }
                }
                //private List<PmDeatil.AssociatedData> pts;
                Elements pTypes = doc.select("PublicationType");
                if (pTypes.size() > 0) {
                    List<PmDeatil.AssociatedData> pts = new ArrayList<>();
                    for (Element pType : pTypes) {
                        PmDeatil.AssociatedData pt = new PmDeatil.AssociatedData();
                        pt.setType(2);
                        pt.setTitle(pType.text());
                        List<PmDeatil.Items> hrefs = new ArrayList<>();
                        hrefs.add(new PmDeatil.Items("Search in PubMed", "?term=" + StringUtil.encodeUrl(pType.text() + "[pt]")));
                        hrefs.add(new PmDeatil.Items("Search in MeSH", "https://www.ncbi.nlm.nih.gov/mesh?term=" + pType.text()));
                        hrefs.add(new PmDeatil.Items("Add to Search", pType.text() + "[pt]"));
                        pt.setHrefs(hrefs);
                        pts.add(pt);
                    }
                    pub.setPts(JSONUtil.toJsonStr(pts));
                }
                //private List<PmDeatil.AssociatedData> meshs;
                //一个展示用，一个程序用

                Elements mes = doc.select("MeshHeadingList MeshHeading");
                if (mes.size() > 0) {
                    String meshHeading = "";
                    String meshHeadingy = "";
                    List<PmDeatil.AssociatedData> meshs = new ArrayList<>();
                    for (Element el : mes) {
                        //展示用
                        String dName = "";
                        if (el.select("DescriptorName").size() > 0) {
                            dName = el.select("DescriptorName").text();
                        }
                        if (el.select("QualifierName").size() > 0) {
                            for (Element qn : el.select("QualifierName")) {
                                PmDeatil.AssociatedData mesh = new PmDeatil.AssociatedData();
                                mesh.setType(2);
                                String qname = dName + " / " + qn.text();
                                mesh.setTitle(qname);
                                String sPubmed = dName + "/" + qn.text() + "[MESH]";
                                if (qn.attr("MajorTopicYN").equals("Y")) {
                                    mesh.setTitle(qname + "*");
                                    sPubmed = dName + "/" + qn.text() + "[MAJR]";
                                }
                                List<PmDeatil.Items> hrefs = new ArrayList<>();
                                hrefs.add(new PmDeatil.Items("Search in PubMed", "?term=" + StringUtil.encodeUrl(sPubmed)));
                                hrefs.add(new PmDeatil.Items("Search in MeSH", "https://www.ncbi.nlm.nih.gov/mesh?term=" + dName.replaceAll(" ", "+")));
                                hrefs.add(new PmDeatil.Items("Add to Search", sPubmed));
                                mesh.setHrefs(hrefs);
                                meshs.add(mesh);
                            }
                        } else {
                            PmDeatil.AssociatedData mesh = new PmDeatil.AssociatedData();
                            mesh.setType(2);
                            mesh.setTitle(dName);
                            List<PmDeatil.Items> hrefs = new ArrayList<>();
                            hrefs.add(new PmDeatil.Items("Search in PubMed", "?term=" + StringUtil.encodeUrl(dName + "[MESH]")));
                            hrefs.add(new PmDeatil.Items("Search in MeSH", "https://www.ncbi.nlm.nih.gov/mesh?term=" + dName));
                            hrefs.add(new PmDeatil.Items("Add to Search", dName + "[MESH]"));
                            mesh.setHrefs(hrefs);
                            meshs.add(mesh);
                        }


                        String m1 = "";
                        String m2 = "";
                        meshHeading += ";" + el.select("DescriptorName").text();
                        //999999

                        String param = "";
                        Element descriptorName = el.select("DescriptorName").first();
                        if (descriptorName.attr("MajorTopicYN").equals("Y")) {
                            param = el.select("DescriptorName").text() + "*";
                        } else {
                            param = el.select("DescriptorName").text();
                        }
                        meshHeadingy += ";" + param;
                        //00000
//                        meshHeadingy+= ";" + el.select("DescriptorName").text();
                        Elements ss = el.select("QualifierName");
                        for (Element els : ss) {
                            m1 += "/" + els.text();
                            if (els.attr("MajorTopicYN").equals("Y")) {
                                m2 += "/*" + els.text();
                            } else {
                                m2 += "/" + els.text();
                            }
                        }
                        if (m1.length() > 0) {
                            m1 = m1.substring(1);
                            meshHeading += "|" + m1;
                        }
                        if (m2.length() > 0) {
                            m2 = m2.substring(1);
                            meshHeadingy += "|" + m2;
                        }
                    }
                    if (StringUtil.isNotNull(meshHeading)) {
                        pub.setMeshHeading(meshHeading.substring(1));
                        pub.setMeshHeadingy(meshHeadingy.substring(1));
                    }
                    pub.setMeshs(JSONUtil.toJsonStr(meshs));
                }
                //private List<PmDeatil.AssociatedData> subs;
                Elements chemical = doc.select("ChemicalList Chemical");
                if (chemical.size() > 0) {
                    List<PmDeatil.AssociatedData> subs = new ArrayList<>();
                    for (Element chem : chemical) {
                        if (chem.select("NameOfSubstance").size() > 0) {
                            String substance = chem.select("NameOfSubstance").text();
                            PmDeatil.AssociatedData sub = new PmDeatil.AssociatedData();
                            sub.setType(2);
                            sub.setTitle(substance);
                            List<PmDeatil.Items> hrefs = new ArrayList<>();
                            hrefs.add(new PmDeatil.Items("Search in PubMed", "?term=" + StringUtil.encodeUrl(substance + "[nm]")));
                            hrefs.add(new PmDeatil.Items("Search in MeSH", "https://www.ncbi.nlm.nih.gov/mesh?term=" + substance));
                            hrefs.add(new PmDeatil.Items("Add to Search", substance + "[nm]"));
                            sub.setHrefs(hrefs);
                            subs.add(sub);
                        }
                    }
                    pub.setSubs(JSONUtil.toJsonStr(subs));
                }
                //private List<PmDeatil.AssociatedData> grants;
                Elements grantList = doc.select("GrantList Grant");
                if (grantList.size() > 0) {
                    List<PmDeatil.AssociatedData> grants = new ArrayList<>();
                    for (Element gran : grantList) {
                        PmDeatil.AssociatedData grant = new PmDeatil.AssociatedData();
                        grant.setType(1);
                        String name = StringUtil.filterHtml(gran.toString().replace("<grant>", "").replace("</grant>", ""));
                        name = StringUtil.filterHtml2(name);
                        name = name.replaceAll("\n", "").replaceAll("  ", "").substring(1);
                        grant.setTitle(name);
                        grant.setHref("/?term=" + StringUtil.encodeUrl(name + "[Grant Number]"));
                        grants.add(grant);
                    }
                    pub.setGrants(JSONUtil.toJsonStr(grants));
                }
                //private List<PmDeatil.AssociatedData> assData;--没法玩啊 无法判断直接跳转还是下拉跳转
                //private List<PmDeatil.FullText> fulltext;--找不到
                //private String source;
                String source = "";
                List<PmDeatil.Author> authors = JSONUtil.toList(JSONUtil.parseArray(pub.getAuthor()), PmDeatil.Author.class);
                if (authors != null && authors.size() > 0) {
                    source = authors.get(0).getName() + ",";
                }
                if (pub.getJour() != null && !pub.getJour().equals("")) {
                    source = source + pub.getJour() + " ";
                }
                if (pub.getPubYear() != null && !pub.getPubYear().equals("")) {
                    source = source + pub.getPubYear() + ".";
                }
                pub.setSource(source);
                //lan
                Elements languages = doc.select("Language");
                if (languages.size() > 0) {
                    String lan = "";
                    for (Element el : languages) {
                        lan += ";" + el.text();
                    }
                    if (lan.length() > 0) {
                        pub.setLan(lan.substring(1));
                    }
                }
                list.add(pub);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    @Data
    public static class AuthorExtend implements Serializable {
        private Integer number;
        private String LastName;
        private String ForeName;
        private String Initials;
        private String IdentifierSource;
        private List<String> Affiliation;
    }
}
